//
//  SEDoctorAuthViewModel.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/6.
//

import UIKit

enum SEDoctorAuthImageType: Int {
    case idCard = 0
    case idCardBack
    case doctorCard
}

class SEDoctorAuthViewModel: SEBaseViewModel {

    var type: SEDoctorAuthViewControllerType = .mine

    var imageType: SEDoctorAuthImageType = .idCard

    var userInfoModel = SEUserInfoModel()

    var rejectionReason: String {
        return "失败原因: " + userInfoModel.rejectionReason
    }

    var uploadVM = SEUploadViewModel(withLoading: true)

    var uploadHandler: ((SEHttpStatus) -> Void)?

    var dataSource: [SEDoctorAuthCellModel] {
        return [
            SEDoctorAuthCellModel(title: "身份证正面照片",
                                  memo: "点击上传身份证正面照片",
                                  imageUrlStr: userInfoModel.cardFrontPath,
                                  placeholder: Asset.doctorauthIdcardImage.image),
            SEDoctorAuthCellModel(title: "身份证反面照片",
                                  memo: "点击上传身份证反面照片",
                                  imageUrlStr: userInfoModel.cardBackPath,
                                  placeholder: Asset.doctorauthIdcardbackImage.image),
            SEDoctorAuthCellModel(title: "执业医师资格证",
                                  memo: "点击上传执业医师资格证照片",
                                  imageUrlStr: userInfoModel.certificatePath,
                                  placeholder: Asset.doctorauthCardImage.image),
        ]
    }

    func uploadImg(image: UIImage) {
        var bucket = ""
        var type = ""
        switch self.imageType {
        case .doctorCard:
            bucket = "E"
            type = "E-1"
        default:
            bucket = "E"
            type = "E-0"
        }
        uploadVM.uploadImage(bucket: bucket, type: type, image: image, size: image.imageSize())
        uploadVM.updateData = { [weak self] status in
            switch status {
            case .success:
                switch self?.imageType {
                case .idCard:
                    self?.userInfoModel.cardFrontId = self?.uploadVM.fileModel?.resourceId ?? ""
                    self?.userInfoModel.cardFrontPath = self?.uploadVM.fileModel?.shareUrl ?? ""
                case .idCardBack:
                    self?.userInfoModel.cardBackId = self?.uploadVM.fileModel?.resourceId ?? ""
                    self?.userInfoModel.cardBackPath = self?.uploadVM.fileModel?.shareUrl ?? ""
                case .doctorCard:
                    self?.userInfoModel.certificateId = self?.uploadVM.fileModel?.resourceId ?? ""
                    self?.userInfoModel.certificatePath = self?.uploadVM.fileModel?.shareUrl ?? ""
                case .none:
                    break
                }
            default:
                break
            }
            self?.uploadHandler?(status)
        }
    }

    var checkSubmit: String? {
        if userInfoModel.cardFrontId.isEmpty {
            return "请上传身份证正面照片"
        }
        if userInfoModel.cardBackId.isEmpty {
            return "请上传身份证反面照片"
        }
        if userInfoModel.certificateId.isEmpty {
            return "请上执业医师资格证照片"
        }
        return nil
    }

    /// 提交医师认证
    func submitDoctorAuth() {
        if let msg = checkSubmit {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.request(SEMineAPI.doctorAuth(id:userInfoModel.id, idcard: userInfoModel.cardFrontId, idcardBack: userInfoModel.cardBackId, card: userInfoModel.certificateId), model: Any.self, withLoading: true) { result, status in
            self.updateData?(status)
        }
    }
}
